﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PhoneBook_PC2
{
    public partial class Groups : Form
    {
        PrincipalFrame principalFrame;
        Book phoneBook;

        public Groups(PrincipalFrame principalFrame, Book phoneBook)
        {
            InitializeComponent();
            this.CenterToScreen();

            this.principalFrame = principalFrame;
            this.phoneBook = phoneBook;
        }


        private void Groups_Load(object sender, EventArgs e)
        {
            this.fillGroupsList();
        }


        private void addGroupButton_Click(object sender, EventArgs e)
        {/*
            //appel de la méthode qui vérifie si le groupe existe déjà
            principalFrame.addGroupToList(addGroupTextBox.Text);
            //appel de la méthode de DLL qui ajoute réellement un groupe
            this.Hide();
          */
        }

        private void fillGroupsList()
        {
            List<String> groupNames = phoneBook.getListGroups();
            foreach (String groupName in groupNames)
            {
                listGroups.Items.Add(groupName);
            }
        }

        private void listGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            listContactsGroup.Items.Clear();
            listOtherContacts.Items.Clear();
            String group = listGroups.SelectedItem.ToString();
            List<PhoneBook.Contact> groupContacts = phoneBook.getListGroupContacts(group);
            List<PhoneBook.Contact> allContacts = phoneBook.getContacts();
            foreach (PhoneBook.Contact contact in groupContacts)
            {
                listContactsGroup.Items.Add(contact.getPerson().getFirstname() + "/" + contact.getPerson().getLastname());
            }
            foreach(PhoneBook.Contact contact in allContacts) {
                if(!groupContacts.Contains(contact))
                    listOtherContacts.Items.Add(contact.getPerson().getFirstname() + "/" + contact.getPerson().getLastname());
            }
        }

        private void buttonAddToGroup_Click(object sender, EventArgs e)
        {
            String contactString = listOtherContacts.SelectedItem.ToString();
            if (!contactString.Equals(""))
            {
                String[] contactInfos = contactString.Split('/');
                String contactFirstName = contactInfos[0];
                String contactLastName = contactInfos[1];
                PhoneBook.Contact contact = phoneBook.getContact(contactFirstName, contactLastName);

                String groupName = listGroups.SelectedItem.ToString();
                PhoneBook.Group group = phoneBook.getGroup(groupName);
                phoneBook.addContactToGroup(group, contact);
                listContactsGroup.Items.Add(contactFirstName + "/" + contactLastName);
                listOtherContacts.Items.Remove(contactFirstName + "/" + contactLastName);
            }
            //Evenement pour rafraichir la list dans PrincipalFrame
        }

        private void buttonRemoveFromGroup_Click(object sender, EventArgs e)
        {
            String contactString = listContactsGroup.SelectedItem.ToString();
            if (!contactString.Equals(""))
            {
                String[] contactInfos = contactString.Split('/');
                String contactFirstName = contactInfos[0];
                String contactLastName = contactInfos[1];
                PhoneBook.Contact contact = phoneBook.getContact(contactFirstName, contactLastName);

                String groupName = listGroups.SelectedItem.ToString();
                PhoneBook.Group group = phoneBook.getGroup(groupName);
                phoneBook.removeContactFromGroup(group, contact);

                listContactsGroup.Items.Remove(contactFirstName + "/" + contactLastName);
                listOtherContacts.Items.Add(contactFirstName + "/" + contactLastName);
            }
        }
    }
}
